package com.wxtf.platform.tools;

import java.io.*;
import java.util.*;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.*;

/**
 * <li>Title: XLSUtils.java</li>
 * <li>Project: baseplatform</li>
 * <li>Package: com.wxtf.platform.tools</li>
 * <li>Description: xls操作类</li>
 * <li>Copyright: Copyright (c) 2012</li>
 * <li>Company: wxthtf Technologies </li>
 * <li>Created on Sep 6, 2012 1:26:57 PM</li>
 *
 * @author chun_chang
 * @version 1.0.0.0
 *
 */
public class XLSUtils{
	
	/** 
	 * 描述：writeXls 写XLS文件内容
	 * @param list 数据集
	 * @param file 写入文件
	 * @param title sheet页标题
	 * @param startRow xls数据起始行
	 * @throws Exception
	 * @CreateOn Sep 7, 2012  9:25:24 AM
	 * @author chun_chang
	 */
	public static void writeXls(List<Object[]> list, File file, String title, int startRow) throws Exception {
		POIFSFileSystem filein = new POIFSFileSystem(new FileInputStream(file));
		HSSFWorkbook book = new HSSFWorkbook(filein);
		HSSFSheet sheet = book.getSheet(title);
		
		if(null != list && list.size() > 0){
			HSSFRow row = null;
			for (int i = 0; i < list.size(); i++) {// 行
				row = sheet.createRow(i + startRow);
				row.setHeight((short) 400);
				
				//序号
				HSSFCell cell = row.createCell(0);
				cell.setCellValue((i+1) + "");
				
				Object[] metaData = list.get(i);
				for(int j = 0; j < metaData.length; j++) {
					cell = row.createCell(j + 1);
					// 如果不是图片数据，就利用正则表达式判断textValue是否全部由数字组成
					String textValue = metaData[j].toString();
					cell.setCellValue(new HSSFRichTextString(textValue));
				}
			}
		}
		
		FileOutputStream fileOut = new FileOutputStream(file);
		book.write(fileOut);
		fileOut.close();
	}
	
	
}
